package com.premedsidekick;


import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class DefaultDatabaseAdapter {
	int DATABASE_VERSION = 1;
	String DATABASE_NAME;
	String KEY_ROWID = "_id";
	String KEY_NAME = "name";
	String KEY_HOURS = "hours";
	Context context;
	SQLiteDatabase database;
	DefaultDatabaseHelper dbHelper;
	
	public DefaultDatabaseAdapter(Context context, String name) {
		this.context = context;
		DATABASE_NAME = name;
	}
	
	public DefaultDatabaseAdapter open() throws SQLException {
		dbHelper = new DefaultDatabaseHelper(context);
		database = dbHelper.getWritableDatabase();
		return this;
	}
	
	public void close() {
		dbHelper.close();
	}
	
	public long create(String name, String hours) {
		ContentValues initialValues = createContentValues(name, hours);
		return database.insert(DATABASE_NAME, null, initialValues);
	}
	
	public boolean update(long rowId, String name, String hours) {
		ContentValues updateValues = createContentValues(name, hours);
		return database.update(DATABASE_NAME, updateValues, KEY_ROWID + "=" + rowId, null) > 0;
	}
	public boolean updateName(long rowId, String name) {
		ContentValues updateValues = createContentName(name);
		return database.update(DATABASE_NAME, updateValues, KEY_ROWID + "="
				+ rowId, null) > 0;
	}
	public boolean updateHours(long rowId, String name) {
		ContentValues updateValues = createContentHours(name);
		return database.update(DATABASE_NAME, updateValues, KEY_ROWID + "="
				+ rowId, null) > 0;
	}
	public boolean delete(long rowId) {
		return database.delete(DATABASE_NAME, KEY_ROWID + "=" + rowId, null) > 0;
	}

	public Cursor getAll() {
		//System.out.println("Database Name: " + DATABASE_NAME);
		Cursor cursor = database.query(DATABASE_NAME, new String[] {KEY_ROWID,
				KEY_NAME, KEY_HOURS}, null, null, null,
				null, null);
		if (cursor.getCount() == 0) {
			for (int i = 0; i < 3; i++) 
				create("Insert name", "0");
			cursor = database.query(DATABASE_NAME, new String[] {KEY_ROWID,
					KEY_NAME, KEY_HOURS}, null, null, null,
					null, null);
		}
		return cursor;
	}
	
	public Cursor getHours() {
		return database.query(DATABASE_NAME, new String[] {
				KEY_HOURS}, null, null, null,
				null, null);
	}

	public boolean delete() {
		return database.delete(DATABASE_NAME, null, null) > 0;
	}	
	
	public class DefaultDatabaseHelper extends SQLiteOpenHelper {
		
		DefaultDatabaseHelper(Context context) {
			super(context, DATABASE_NAME, null, DATABASE_VERSION);
		}
	
		@Override
		public void onCreate(SQLiteDatabase db) {
			db.execSQL("CREATE TABLE " + DATABASE_NAME + " (_id INTEGER PRIMARY KEY AUTOINCREMENT, " +
			"name TEXT, hours TEXT);");
		}
	
		@Override
		public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
			// TODO Auto-generated method stub
			
		}
	}
	public ContentValues createContentValues(String name, String hours) {
		ContentValues values = new ContentValues();
		values.put(KEY_NAME, name);
		values.put(KEY_HOURS, hours);
		return values;
	}
	public ContentValues createContentName(String name) {
		ContentValues values = new ContentValues();
		values.put(KEY_NAME, name);
		return values;
	}
	public ContentValues createContentHours(String hours) {
		ContentValues values = new ContentValues();
		values.put(KEY_HOURS, hours);
		return values;
	}
		
}
